﻿using System;
using System.Linq;
using System.Collections.Generic;
using NUnit.Framework;
using AutoMapper;

namespace NMA.Infrastructure.NHibernate.Tests.Repository
{
    using NMA.Domain.Model;
    using NMA.Infrastructure.NHibernate.DomainObject;

    [TestFixture]
    public class ResourceRepositoryTest : TestingBase
    {
        [Test]
        public void Can_count_all_resources()
        {
            int result = -1;

            if (ResourceRepository != null)
            {
                result = ResourceRepository.All().Count();

                Assert.IsTrue(result >= 0, "contain " + result + " item in database");
            }
        }

        [Test]
        public void Can_get_by_id()
        {
            IResources entity = null;

            if (ResourceRepository != null)
            {
                var obj = ResourceList.FirstOrDefault<Resources>();

                entity = ResourceRepository.GetById(obj.Id);

                Assert.IsNotNull(entity, "Resources is not null");
                Assert.IsTrue(entity.Id == obj.Id);
                Assert.IsTrue(entity.ResourceType == obj.ResourceType);
                Assert.IsTrue(entity.CultureCode == obj.CultureCode);
                Assert.IsTrue(entity.ResourceKey == obj.ResourceKey);
                Assert.IsTrue(entity.ResourceValue == obj.ResourceValue);
                Assert.IsTrue(entity.CreatedDate.ToShortDateString() == obj.CreatedDate.ToShortDateString());
            }
        }

        [Test]
        public void Update_resources_to_database()
        {
            if (ResourceRepository != null)
            {
                var entity = ResourceList.FirstOrDefault<Resources>();

                var dbObj = ResourceRepository.GetById(entity.Id);

                Assert.IsNotNull(dbObj);

                dbObj.ResourceType = "update resources type";
                dbObj.UpdatedDate = DateTime.Now.AddDays(1);

                // update into database
                ResourceRepository.Update(dbObj);

                // get out for assertion
                var updateObj = ResourceRepository.GetById(dbObj.Id);

                Assert.IsNotNull(updateObj);
                Assert.IsTrue(updateObj.Id == dbObj.Id);
                Assert.IsTrue(updateObj.ResourceType == "update resources type");
                Assert.IsTrue(updateObj.UpdatedDate.ToShortDateString() == DateTime.Now.AddDays(1).ToShortDateString());
            }
        }
    }
}